package com.atguigu.gulimall.order.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.atguigu.gulimall.order.service.OrderItemService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@RabbitListener(queues = {"hello-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }


    @RabbitHandler
    public void receiveMessage(Message message,
                               OrderReturnReasonEntity content,
                               Channel channel) throws IOException {
        System.out.println("收到消息..." + content);
        byte[] body = message.getBody();
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag==" + deliveryTag);
        //确认消息
        if (deliveryTag % 2 == 0) {
            channel.basicAck(deliveryTag, false);
            System.out.println("消费了：" + deliveryTag);
        } else {
            channel.basicNack(deliveryTag, false, false);
            System.out.println("拒绝了：" + deliveryTag);
        }
        System.out.println("消息处理完成：" + content.getName());
    }

    @RabbitHandler
    public void receiveMessage(Message message, OrderEntity content, Channel channel) throws IOException {
        System.out.println("收到消息..." + content);
        byte[] body = message.getBody();
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag==" + deliveryTag);
        //确认消息
        if (deliveryTag % 2 == 0) {
            channel.basicAck(deliveryTag, false);
            System.out.println("消费了：" + deliveryTag);
        } else {
            channel.basicNack(deliveryTag, false, true);
            System.out.println("拒绝了：" + deliveryTag);
        }
        System.out.println("消息处理完成：" + content.getOrderSn());
        System.out.println("接收到的消息内容：" + content + "==>消息类型：" + content.getOrderSn());
    }
}